package com.uranus.wallet.trade.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uranus.common.VO.ServiceContext;
import com.uranus.wallet.trade.service.TradeService;
import com.uranus.common.VO.Result;
import com.uranus.common.VO.paging.PagingRequest;
import com.uranus.common.bean.expection.BusinessException;
import com.uranus.wallet.trade.beans.Trade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wei_yzh
 * @since 2018-12-13
 */
@Api(description = "交易信息")
@RestController
@RequestMapping("/trade")
public class TradeController {

    @Autowired
    TradeService tradeService;

    @ApiOperation(value = "get trades list by from userId", notes = "params service")
    @PostMapping("/trades/from")
    public Result getTradesByFrom(@RequestBody PagingRequest request) {
        Result result = new Result();

       int from_userId = ServiceContext.getInstance().getUserId();
        //int from_userId =30;

        int pageIndex = request.getPaginator().getPage();
        int pageSize = request.getPaginator().getPageSize();

        try {
            Page<Trade> page = new Page<>(pageIndex, pageSize);
            IPage<Trade> pageInfo=tradeService.page(page,new QueryWrapper<Trade>().
                    eq("t_from_id",from_userId)
                    .orderByDesc("id"));
            result.setSuccess(true);
            result.setData(pageInfo);
        } catch (BusinessException e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;
    }

}

